Skip to content

Add RoundingRule enum and optional rounding parameter to Fix64/UFix64 conversion functions#4474

Open
turbolent wants to merge 2 commits intomasterfrom
bastian/fixed-point-rounding
Open

Add RoundingRule enum and optional rounding parameter to Fix64/UFix64 conversion functions#4474
turbolent wants to merge 2 commits intomasterfrom
bastian/fixed-point-rounding

Conversation

@turbolent
Copy link
Copy Markdown
Member

@turbolent turbolent commented Apr 18, 2026

Description

  • Add new built-in RoundingRule enum type
  • Add optional rounding: RoundingRule parameter to Fix64() and UFix64() conversion functions, enabling control over precision loss when converting from Fix128/UFix128. Without the parameter, behavior is unchanged (truncation toward zero)
  • Rename CryptoAlgorithm interface to NativeEnumCase and extract generic enum helpers into stdlib/enum.go

  • Targeted PR against master branch
  • Linked to Github issue with discussion and accepted design OR link to spec that describes this work
  • Code follows the standards mentioned here
  • Updated relevant documentation
  • Re-reviewed Files changed in the Github PR explorer
  • Added appropriate labels

@turbolent turbolent self-assigned this Apr 18, 2026
@turbolent turbolent requested a review from SupunS as a code owner April 18, 2026 00:11
@github-actions
Copy link
Copy Markdown

github-actions Bot commented Apr 18, 2026

Dependency Review

✅ No vulnerabilities or license issues or OpenSSF Scorecard issues found.

Scanned Files

None

@github-actions
Copy link
Copy Markdown

github-actions Bot commented Apr 18, 2026

Benchstat comparison

  • Base branch: onflow:master
  • Base commit: e8fe69b
Results

old.txtnew.txt
time/opdelta
pkg:github.com/onflow/cadence/interpreter goos:linux goarch:amd64
ByteArrayTransfer-41.18µs ± 0%1.23µs ± 0%~(p=1.000 n=1+1)
ByteArrayValueToByteSlice-484.0ns ± 0%84.9ns ± 0%~(p=1.000 n=1+1)
ByteSliceToByteArrayValue-4950ns ± 0%996ns ± 0%~(p=1.000 n=1+1)
pkg:github.com/onflow/cadence/bbq/compiler goos:linux goarch:amd64
CompileFungibleTokenTransferTransaction-4127µs ± 0%121µs ± 0%~(p=1.000 n=1+1)
CompileTime-429.6µs ± 0%29.3µs ± 0%~(p=1.000 n=1+1)
pkg:github.com/onflow/cadence/runtime goos:linux goarch:amd64
ContractFunctionInvocation-4346µs ± 0%350µs ± 0%~(p=1.000 n=1+1)
pkg:github.com/onflow/cadence/bbq/vm/test goos:linux goarch:amd64
ContractImport-4204µs ± 0%200µs ± 0%~(p=1.000 n=1+1)
pkg:github.com/onflow/cadence/interpreter goos:linux goarch:amd64
EMVAddressTransfer-43.38µs ± 0%3.64µs ± 0%~(p=1.000 n=1+1)
Emit-44.48ms ± 0%4.43ms ± 0%~(p=1.000 n=1+1)
EnumTransfer-41.39µs ± 0%1.33µs ± 0%~(p=1.000 n=1+1)
pkg:github.com/onflow/cadence/runtime goos:linux goarch:amd64
ExportType/composite_type-4247ns ± 0%245ns ± 0%~(p=1.000 n=1+1)
ExportType/simple_type-476.8ns ± 0%74.6ns ± 0%~(p=1.000 n=1+1)
pkg:github.com/onflow/cadence/bbq/vm/test goos:linux goarch:amd64
FTTransfer-4129µs ± 0%130µs ± 0%~(p=1.000 n=1+1)
pkg:github.com/onflow/cadence/sema goos:linux goarch:amd64
FlowTokenContract-4580µs ± 0%570µs ± 0%~(p=1.000 n=1+1)
pkg:github.com/onflow/cadence/interpreter goos:linux goarch:amd64
ImperativeFib-419.8µs ± 0%20.2µs ± 0%~(p=1.000 n=1+1)
pkg:github.com/onflow/cadence/bbq/vm/test goos:linux goarch:amd64
ImperativeFib-422.2µs ± 0%22.6µs ± 0%~(p=1.000 n=1+1)
ImperativeFibNewCompilerNewVM-446.0µs ± 0%44.7µs ± 0%~(p=1.000 n=1+1)
ImperativeFibNewVM-425.1µs ± 0%25.6µs ± 0%~(p=1.000 n=1+1)
pkg:github.com/onflow/cadence/interpreter goos:linux goarch:amd64
InterpretRecursionFib-42.20ms ± 0%2.20ms ± 0%~(p=1.000 n=1+1)
pkg:github.com/onflow/cadence/bbq/vm/test goos:linux goarch:amd64
InterpreterFTTransfer-4110µs ± 0%106µs ± 0%~(p=1.000 n=1+1)
InterpreterImperativeFib-421.0µs ± 0%19.9µs ± 0%~(p=1.000 n=1+1)
InterpreterNewStruct-464.3µs ± 0%63.1µs ± 0%~(p=1.000 n=1+1)
MethodCall/concrete_type_method_call-440.5µs ± 0%40.0µs ± 0%~(p=1.000 n=1+1)
MethodCall/interface_method_call-456.2µs ± 0%56.0µs ± 0%~(p=1.000 n=1+1)
pkg:github.com/onflow/cadence/interpreter goos:linux goarch:amd64
NewInterpreter/new_interpreter-4852ns ± 0%850ns ± 0%~(p=1.000 n=1+1)
NewInterpreter/new_sub-interpreter-4321ns ± 0%324ns ± 0%~(p=1.000 n=1+1)
pkg:github.com/onflow/cadence/bbq/vm/test goos:linux goarch:amd64
NewResource-489.9µs ± 0%91.2µs ± 0%~(p=1.000 n=1+1)
NewStruct-444.0µs ± 0%43.1µs ± 0%~(p=1.000 n=1+1)
NewStructRaw-43.66µs ± 0%3.17µs ± 0%~(p=1.000 n=1+1)
pkg:github.com/onflow/cadence/sema goos:linux goarch:amd64
QualifiedIdentifierCreation/One_level-42.12ns ± 0%2.12ns ± 0%~(all equal)
QualifiedIdentifierCreation/Three_levels-479.4ns ± 0%79.8ns ± 0%~(p=1.000 n=1+1)
pkg:github.com/onflow/cadence/bbq/vm/test goos:linux goarch:amd64
RecursionFib-41.42ms ± 0%1.39ms ± 0%~(p=1.000 n=1+1)
RuntimeFungibleTokenTransfer-4764µs ± 0%751µs ± 0%~(p=1.000 n=1+1)
pkg:github.com/onflow/cadence/runtime goos:linux goarch:amd64
RuntimeFungibleTokenTransferInterpreter-4605µs ± 0%591µs ± 0%~(p=1.000 n=1+1)
RuntimeFungibleTokenTransferVM-4704µs ± 0%678µs ± 0%~(p=1.000 n=1+1)
RuntimeResourceDictionaryValues-42.61ms ± 0%2.61ms ± 0%~(p=1.000 n=1+1)
RuntimeResourceTracking-49.41ms ± 0%9.32ms ± 0%~(p=1.000 n=1+1)
RuntimeScriptNoop-413.7µs ± 0%14.3µs ± 0%~(p=1.000 n=1+1)
RuntimeVMInvokeContractImperativeFib-436.8µs ± 0%38.6µs ± 0%~(p=1.000 n=1+1)
pkg:github.com/onflow/cadence/sema goos:linux goarch:amd64
SuperTypeInference/arrays-4219ns ± 0%218ns ± 0%~(p=1.000 n=1+1)
SuperTypeInference/composites-4120ns ± 0%86ns ± 0%~(p=1.000 n=1+1)
SuperTypeInference/integers-4333ns ± 0%331ns ± 0%~(p=1.000 n=1+1)
pkg:github.com/onflow/cadence/interpreter goos:linux goarch:amd64
ValueIsSubtypeOfSemaType-459.0ns ± 0%59.2ns ± 0%~(p=1.000 n=1+1)
 
alloc/opdelta
pkg:github.com/onflow/cadence/interpreter goos:linux goarch:amd64
ByteArrayTransfer-41.08kB ± 0%1.08kB ± 0%~(p=1.000 n=1+1)
ByteArrayValueToByteSlice-432.0B ± 0%32.0B ± 0%~(all equal)
ByteSliceToByteArrayValue-4852B ± 0%862B ± 0%~(p=1.000 n=1+1)
pkg:github.com/onflow/cadence/bbq/compiler goos:linux goarch:amd64
CompileFungibleTokenTransferTransaction-482.9kB ± 0%82.9kB ± 0%~(all equal)
CompileTime-416.9kB ± 0%16.9kB ± 0%~(all equal)
pkg:github.com/onflow/cadence/runtime goos:linux goarch:amd64
ContractFunctionInvocation-4144kB ± 0%145kB ± 0%~(p=1.000 n=1+1)
pkg:github.com/onflow/cadence/bbq/vm/test goos:linux goarch:amd64
ContractImport-475.2kB ± 0%75.0kB ± 0%~(p=1.000 n=1+1)
pkg:github.com/onflow/cadence/interpreter goos:linux goarch:amd64
EMVAddressTransfer-42.45kB ± 0%2.45kB ± 0%~(p=1.000 n=1+1)
Emit-41.49MB ± 0%1.49MB ± 0%~(p=1.000 n=1+1)
EnumTransfer-4827B ± 0%833B ± 0%~(p=1.000 n=1+1)
pkg:github.com/onflow/cadence/runtime goos:linux goarch:amd64
ExportType/composite_type-4120B ± 0%120B ± 0%~(all equal)
ExportType/simple_type-40.00B 0.00B ~(all equal)
pkg:github.com/onflow/cadence/bbq/vm/test goos:linux goarch:amd64
FTTransfer-443.6kB ± 0%43.6kB ± 0%~(p=1.000 n=1+1)
pkg:github.com/onflow/cadence/sema goos:linux goarch:amd64
FlowTokenContract-4226kB ± 0%226kB ± 0%~(p=1.000 n=1+1)
pkg:github.com/onflow/cadence/interpreter goos:linux goarch:amd64
ImperativeFib-48.30kB ± 0%8.30kB ± 0%~(all equal)
pkg:github.com/onflow/cadence/bbq/vm/test goos:linux goarch:amd64
ImperativeFib-45.41kB ± 0%5.41kB ± 0%~(all equal)
ImperativeFibNewCompilerNewVM-425.8kB ± 0%25.8kB ± 0%~(all equal)
ImperativeFibNewVM-48.10kB ± 0%8.10kB ± 0%~(all equal)
pkg:github.com/onflow/cadence/interpreter goos:linux goarch:amd64
InterpretRecursionFib-41.19MB ± 0%1.19MB ± 0%~(p=1.000 n=1+1)
pkg:github.com/onflow/cadence/bbq/vm/test goos:linux goarch:amd64
InterpreterFTTransfer-441.9kB ± 0%41.9kB ± 0%~(p=1.000 n=1+1)
InterpreterImperativeFib-48.29kB ± 0%8.29kB ± 0%~(all equal)
InterpreterNewStruct-424.9kB ± 0%24.7kB ± 0%~(p=1.000 n=1+1)
MethodCall/concrete_type_method_call-410.4kB ± 0%10.3kB ± 0%~(p=1.000 n=1+1)
MethodCall/interface_method_call-417.1kB ± 0%17.1kB ± 0%~(p=1.000 n=1+1)
pkg:github.com/onflow/cadence/interpreter goos:linux goarch:amd64
NewInterpreter/new_interpreter-4976B ± 0%976B ± 0%~(all equal)
NewInterpreter/new_sub-interpreter-4232B ± 0%232B ± 0%~(all equal)
pkg:github.com/onflow/cadence/bbq/vm/test goos:linux goarch:amd64
NewResource-444.0kB ± 0%44.0kB ± 0%~(all equal)
NewStruct-413.4kB ± 0%13.4kB ± 0%~(p=1.000 n=1+1)
NewStructRaw-41.83kB ± 0%1.71kB ± 0%~(p=1.000 n=1+1)
pkg:github.com/onflow/cadence/sema goos:linux goarch:amd64
QualifiedIdentifierCreation/One_level-40.00B 0.00B ~(all equal)
QualifiedIdentifierCreation/Three_levels-464.0B ± 0%64.0B ± 0%~(all equal)
pkg:github.com/onflow/cadence/bbq/vm/test goos:linux goarch:amd64
RecursionFib-4283kB ± 0%283kB ± 0%~(p=1.000 n=1+1)
RuntimeFungibleTokenTransfer-4241kB ± 0%241kB ± 0%~(p=1.000 n=1+1)
pkg:github.com/onflow/cadence/runtime goos:linux goarch:amd64
RuntimeFungibleTokenTransferInterpreter-4165kB ± 0%165kB ± 0%~(p=1.000 n=1+1)
RuntimeFungibleTokenTransferVM-4188kB ± 0%188kB ± 0%~(p=1.000 n=1+1)
RuntimeResourceDictionaryValues-41.76MB ± 0%1.76MB ± 0%~(p=1.000 n=1+1)
RuntimeResourceTracking-46.97MB ± 0%6.97MB ± 0%~(p=1.000 n=1+1)
RuntimeScriptNoop-48.07kB ± 0%8.09kB ± 0%~(p=1.000 n=1+1)
RuntimeVMInvokeContractImperativeFib-413.4kB ± 0%13.4kB ± 0%~(all equal)
pkg:github.com/onflow/cadence/sema goos:linux goarch:amd64
SuperTypeInference/arrays-472.0B ± 0%72.0B ± 0%~(all equal)
SuperTypeInference/composites-40.00B 0.00B ~(all equal)
SuperTypeInference/integers-40.00B 0.00B ~(all equal)
pkg:github.com/onflow/cadence/interpreter goos:linux goarch:amd64
ValueIsSubtypeOfSemaType-432.0B ± 0%32.0B ± 0%~(all equal)
 
allocs/opdelta
pkg:github.com/onflow/cadence/interpreter goos:linux goarch:amd64
ByteArrayTransfer-47.00 ± 0%7.00 ± 0%~(all equal)
ByteArrayValueToByteSlice-41.00 ± 0%1.00 ± 0%~(all equal)
ByteSliceToByteArrayValue-45.00 ± 0%5.00 ± 0%~(all equal)
pkg:github.com/onflow/cadence/bbq/compiler goos:linux goarch:amd64
CompileFungibleTokenTransferTransaction-4576 ± 0%576 ± 0%~(all equal)
CompileTime-4209 ± 0%209 ± 0%~(all equal)
pkg:github.com/onflow/cadence/runtime goos:linux goarch:amd64
ContractFunctionInvocation-42.25k ± 0%2.26k ± 0%~(p=1.000 n=1+1)
pkg:github.com/onflow/cadence/bbq/vm/test goos:linux goarch:amd64
ContractImport-41.29k ± 0%1.29k ± 0%~(all equal)
pkg:github.com/onflow/cadence/interpreter goos:linux goarch:amd64
EMVAddressTransfer-429.0 ± 0%29.0 ± 0%~(all equal)
Emit-440.0k ± 0%40.0k ± 0%~(all equal)
EnumTransfer-413.0 ± 0%13.0 ± 0%~(all equal)
pkg:github.com/onflow/cadence/runtime goos:linux goarch:amd64
ExportType/composite_type-43.00 ± 0%3.00 ± 0%~(all equal)
ExportType/simple_type-40.00 0.00 ~(all equal)
pkg:github.com/onflow/cadence/bbq/vm/test goos:linux goarch:amd64
FTTransfer-41.40k ± 0%1.40k ± 0%~(all equal)
pkg:github.com/onflow/cadence/sema goos:linux goarch:amd64
FlowTokenContract-43.58k ± 0%3.58k ± 0%~(all equal)
pkg:github.com/onflow/cadence/interpreter goos:linux goarch:amd64
ImperativeFib-4176 ± 0%176 ± 0%~(all equal)
pkg:github.com/onflow/cadence/bbq/vm/test goos:linux goarch:amd64
ImperativeFib-4266 ± 0%266 ± 0%~(all equal)
ImperativeFibNewCompilerNewVM-4473 ± 0%473 ± 0%~(all equal)
ImperativeFibNewVM-4308 ± 0%308 ± 0%~(all equal)
pkg:github.com/onflow/cadence/interpreter goos:linux goarch:amd64
InterpretRecursionFib-417.7k ± 0%17.7k ± 0%~(all equal)
pkg:github.com/onflow/cadence/bbq/vm/test goos:linux goarch:amd64
InterpreterFTTransfer-4916 ± 0%916 ± 0%~(all equal)
InterpreterImperativeFib-4175 ± 0%175 ± 0%~(all equal)
InterpreterNewStruct-4418 ± 0%418 ± 0%~(all equal)
MethodCall/concrete_type_method_call-4354 ± 0%354 ± 0%~(all equal)
MethodCall/interface_method_call-4464 ± 0%464 ± 0%~(all equal)
pkg:github.com/onflow/cadence/interpreter goos:linux goarch:amd64
NewInterpreter/new_interpreter-415.0 ± 0%15.0 ± 0%~(all equal)
NewInterpreter/new_sub-interpreter-44.00 ± 0%4.00 ± 0%~(all equal)
pkg:github.com/onflow/cadence/bbq/vm/test goos:linux goarch:amd64
NewResource-4951 ± 0%951 ± 0%~(all equal)
NewStruct-4388 ± 0%388 ± 0%~(all equal)
NewStructRaw-430.0 ± 0%30.0 ± 0%~(all equal)
pkg:github.com/onflow/cadence/sema goos:linux goarch:amd64
QualifiedIdentifierCreation/One_level-40.00 0.00 ~(all equal)
QualifiedIdentifierCreation/Three_levels-42.00 ± 0%2.00 ± 0%~(all equal)
pkg:github.com/onflow/cadence/bbq/vm/test goos:linux goarch:amd64
RecursionFib-415.8k ± 0%15.8k ± 0%~(all equal)
RuntimeFungibleTokenTransfer-44.09k ± 0%4.09k ± 0%~(all equal)
pkg:github.com/onflow/cadence/runtime goos:linux goarch:amd64
RuntimeFungibleTokenTransferInterpreter-43.08k ± 0%3.08k ± 0%~(all equal)
RuntimeFungibleTokenTransferVM-43.61k ± 0%3.61k ± 0%~(all equal)
RuntimeResourceDictionaryValues-436.7k ± 0%36.7k ± 0%~(p=1.000 n=1+1)
RuntimeResourceTracking-4129k ± 0%129k ± 0%~(p=1.000 n=1+1)
RuntimeScriptNoop-4114 ± 0%114 ± 0%~(all equal)
RuntimeVMInvokeContractImperativeFib-4426 ± 0%426 ± 0%~(all equal)
pkg:github.com/onflow/cadence/sema goos:linux goarch:amd64
SuperTypeInference/arrays-43.00 ± 0%3.00 ± 0%~(all equal)
SuperTypeInference/composites-40.00 0.00 ~(all equal)
SuperTypeInference/integers-40.00 0.00 ~(all equal)
pkg:github.com/onflow/cadence/interpreter goos:linux goarch:amd64
ValueIsSubtypeOfSemaType-41.00 ± 0%1.00 ± 0%~(all equal)
 

@turbolent turbolent changed the title Add RoundingMode enum and optional rounding parameter to Fix64/UFix64 conversion functions Add RoundingRule enum and optional rounding parameter to Fix64/UFix64 conversion functions Apr 20, 2026
Copy link
Copy Markdown
Member

@SupunS SupunS left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! 👏

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants